package org.giovanolli.mumi.controller.geo;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.entities.City;
import org.giovanolli.mumi.entities.filter.CityFilter;
import org.giovanolli.mumi.entities.filter.RegionFilter;
import org.giovanolli.mumi.service.CityService;
import org.giovanolli.mumi.service.RegionService;
import org.giovanolli.mumi.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("cityController")
public class CityController extends AbstractController<City> {

	@Autowired
	private CityService service;

	@Autowired
	private RegionService regionService;

	@Autowired
	private CityModel model;

	@Override
	protected void bindEntityToModel() {
		City editedCity = model.getEditedEntity();
		model.setName(editedCity.getName());
		model.setRegion(editedCity.getRegion());
	}

	@Override
	protected void bindModelToEntity() {
		City editedCity = model.getEditedEntity();
		editedCity.setName(model.getName().toUpperCase());
		editedCity.setRegion(model.getRegion());
	}

	protected Class<City> getEntityClass() {
		return City.class;
	}

	@Override
	protected AbstractModel<City> getModel() {
		return model;

	}

	@Override
	protected Service<City> getService() {
		return service;
	}

	public void init() {
		super.init();
		model.setFilter(new CityFilter());
		populateNonEntityPropertiesToModel();
	}

	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectableRegions(regionService.getList(new RegionFilter()));
	}

}
